package org.adit.spring.hibernate.controller;

import java.util.ArrayList;
import java.util.List;

import org.adit.spring.hibernate.component.RouteSession;
import org.adit.spring.hibernate.dao.ResourceTypeDao;
import org.adit.spring.hibernate.dao.RouteDao;
import org.adit.spring.hibernate.entity.ResourceType;
import org.adit.spring.hibernate.entity.Route;
import org.adit.spring.hibernate.infra.Restrito;
import org.adit.spring.hibernate.service.XmlService;
import org.springframework.scheduling.annotation.Scheduled;

import br.com.caelum.vraptor.Delete;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Put;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;

@Resource
public class ResourceTypesController {

	private ResourceTypeDao dao;
	private RouteDao routeDao;
	private RouteSession routeSession;
	private final Result result;
	private XmlService xmlService;

	public ResourceTypesController(XmlService xmlService, RouteSession routeSession, ResourceTypeDao dao, Result result, RouteDao routeDao) {
		this.xmlService = xmlService;
		this.routeSession = routeSession;
		this.dao = dao;
		this.routeDao = routeDao;
		this.result = result;
	}

	@Restrito
	@Post
	@Path("/resourceTypes")
	public void adiciona(ResourceType resourceType) {
		Route route = new Route();
		route = routeDao.carrega(resourceType.getRoute().getId());
		resourceType.setRoute(route);
		dao.salva(resourceType);
		List<ResourceType> resourceTypes = new ArrayList<ResourceType>();
		resourceTypes.add(resourceType);
		route.setResourceTypes(resourceTypes);
		routeDao.salva(route);
		routeSession.setRoute(route);
		result.redirectTo(ResourceTypesController.class).lista(resourceType.getRoute().getId());
	}

	@Restrito
	@Get
	@Path("/resourceTypes/novo/{id}")
	public Route formulario(Long id) {
		Route route = new Route();
		route.setId(id);
		return route;
	}

	@Restrito
	@Get
	@Path("/resourceTypes/{id}")
	public ResourceType edita(Long id) {
		return dao.carrega(id);
	}

	@Restrito
	@Put
	@Path("/resourceTypes/{resourceType.id}")
	public void altera(ResourceType resourceType) {
		dao.atualiza(resourceType);
		result.redirectTo(ResourceTypesController.class).lista(resourceType.getRoute().getId());
	}

	@Restrito
	@Delete
	@Path("/resourceTypes/{id}")
	public void remove(Long id) {
		ResourceType resourceType = dao.carrega(id);
		Route route = new Route();
		route.setId(resourceType.getId());
		dao.remove(resourceType);
		result.redirectTo(ResourceTypesController.class).lista(route.getId());
	}

	@Get
	@Path("/resourceTypes")
	public List<ResourceType> lista(Long id) {
		if (id != null) {
			Route route = new Route();
			route = routeDao.carrega(id);
			routeSession.setRoute(route);
		}
		return dao.listaTudo();
	}

	@Get
	@Path("/resourceTypes/busca")
	public List<ResourceType> busca() {
		return dao.listaTudo();
	}

	public List<ResourceType> busca(String resourceType) {
		result.include("resourceType", resourceType);
		return dao.busca(resourceType);
	}

	@Scheduled(fixedDelay = 5000)
	public void sched() {
		System.out.println("spring eh foda!!!!!!!!!!!!");

	}
	@Get
	@Path("/resourceTypes/busca.json")
	public void buscaJson(String q) {
		result.use(Results.json()).from(dao.busca(q)).exclude("id", "resourceType").serialize();
	}
}